<template>
    <div>
        <rui-page :defines="defines">
            <rui-query :label-width="180"
                       ref="searchForm"
                       :query="queryData"
                       :query-rows="searchFormOptions"
                       :more-rows="searchFormOptionsRows"
            >
                <template slot="createTimes" slot-scope="{ key,data }">
                    <DatePicker v-model="data[key]" type="daterange" format="yyyy-MM-dd" clearable
                                @on-change="selectCreateDateRange" placeholder="选择起始时间"
                                style="width: 200px"></DatePicker>
                </template>
                <template slot="endDates" slot-scope="{ key,data }">
                    <DatePicker v-model="data[key]" type="daterange" format="yyyy-MM-dd" clearable
                                @on-change="selectEndDateRange" placeholder="选择起始时间"
                                style="width: 200px"></DatePicker>
                </template>
            </rui-query>
            <rui-table
                :defineId="'overpaymentDifferenceDefine'"
                :fixedLeft="['action']"
                :showIndex="false"
                :select="false"
                :columns="overpaymentDifferenceColumns"
                :slots="[{key:'action',slot:'action'},{key:'SMS',slot:'SMS'},{key: 'contractNo',slot: 'contractNo'}]"
                @loadDatas="queryData"
                ref-query="searchForm"
                ref="overpaymentTable"
            >
                <!--                <template slot="toolBar">-->
                <!--                    <Button @click="" type="primary">导出报表</Button>-->
                <!--                </template>-->
                <template slot-scope="{ row }" slot="action">
                    <!--
                        处理  isViewBtn ~~审核中 待补件 待补件审核~~
                        发起溢缴款 草稿
                    -->
                    <Button :disabled="!row.isViewBtn" type="primary" size="small"
                            @click="edit('process',row)">处理
                    </Button>

                    <Button :disabled="row.processStatus !== 'draft'" type="primary" size="small"
                            @click="startOverflow(row)">发起溢缴款
                    </Button>
                    <Button :disabled="row.closeDate != undefined" type="primary" size="small"
                            @click="closeContract(row)">合同关闭
                    </Button>
                </template>
                <template slot="contractNo" slot-scope="{ row }">
                    <a @click="edit('read',row)">{{row.contractNo}}</a>
                </template>
            </rui-table>
        </rui-page>
    </div>
</template>
<script>
    import sendMessages from "./components/modal/sendMessages";
    import overpaymentDifferenceDefine from "./define/overpayment-difference-define";
    import {closeContract, startOverflow,findContractCloseList} from "_p/basic/api/contract/contract-close/index";
    //短信发送
    import {
        sendMessageInfo
    } from "@/projects/basic/api/contract/message-template-config/messageTemplate";

    export default {
        components: {
            sendMessages
        },
        data() {
            return {
                pageNo: 1,
                Obj: {},
                // 弹窗状态
                modalTitle: "",
                modal: false,
                searchFormOptions: [
                    {
                        defineId: "overpaymentDifferenceDefine",
                        fields: ["applyNo", "contractNo", "custName"]
                    }
                ],
                searchFormOptionsRows: [
                    {
                        defineId: "overpaymentDifferenceDefine",
                        fields: ["createTimes", "endDates", "processStatus", "processMode", "contractStatus"],
                        slots: [{key: 'createTimes', slotName: 'createTimes'},
                            {key: 'endDates', slotName: 'endDates'}
                        ]
                    }
                ],
                overpaymentDifferenceColumns: [
                    'action', 'caseNo', 'contractStatus', 'applyNo', 'custName', 'contactNo', 'contractNo', 'processMode',
                    'requestDate', 'processStatus', 'endDate', 'amount', 'closeDate'],
                selectCount: 0,
            };
        },
        computed: {
            defines() {
                return [
                    {
                        id: "overpaymentDifferenceDefine",
                        fields: overpaymentDifferenceDefine
                    }
                ];
            }
        },
        mounted() {
        },
        activated() {
            this.refresh();
        },
        methods: {
            selectCreateDateRange(v) {
                if (v) {
                    this.$refs.searchForm.setFiledValue("startCreateDate", v[0]);
                    this.$refs.searchForm.setFiledValue("endCreateDate", v[1]);
                }
            },
            selectEndDateRange(v) {
                if (v) {
                    this.$refs.searchForm.setFiledValue("startFinishDate", v[0]);
                    this.$refs.searchForm.setFiledValue("endFinishDate", v[1]);
                }
            },
            queryData(queryData) {
                if (queryData.condition.contractStatus === '') {
                    queryData.condition.contractStatus = null;
                }
                if (queryData.condition.processMode === '') {
                    queryData.condition.processMode = null;
                }
                findContractCloseList(queryData).then(res => {
                    if (res.code === "0000") {
                        let {records, total} = res.data;
                        this.$refs.overpaymentTable.updateTableData(records, total);
                    }
                });
            },
            startOverflow(row){
                startOverflow(row).then(
                    res=>{
                        if (res.code === "0000") {
                            this.edit('write',row)
                        }
                    }
                )
            },
            refresh() {
                this.$refs['overpaymentTable'].reloadData();
                this.clearSelectAll();
            },
            clearSelectAll() {
                this.$refs.overpaymentTable.getTable().selectAll(false);
            },
            sendMessages(contract) {
                this.modal = true;
                this.modalTitle = "发送短信";
                this.Obj = {
                    contactNo: contract.contactNo,
                    contractNo: contract.contractNo,
                    custName: contract.custName
                }
            },
            closeModal() {
                this.modal = false;
                this.modalTitle = null;
            },
            closeContract(row) {
                if (row.processStatus === 'approved') {
                    closeContract(row).then(res => {
                        if (res.code === "0000") {
                            this.sendMessageInfo();
                            this.refresh();
                        }
                    });
                } else {
                    this.$Modal.warning({
                        title: "警告",
                        content: "请先处理溢缴款，再进行合同关闭!"
                    });
                }
            },
            edit(type, params) {
                params.readOnly = type;
                this.afs.newTab(
                    this,
                    "projects/basic/pages/contract/contract-close/contract-close-detial",
                    "合同关闭详情",
                    "ios-add",
                    params,
                    "contract-close-detial-" + this.pageNo,
                    [],
                    true
                );
                this.pageNo += 1;
            },
            sendMessageInfo() {
                let params = {
                    name: this.Obj.custName,
                    number: this.Obj.contactNo,
                    contractNo: this.Obj.contractNo,
                    templateType: 'short-msg',
                    templateBusiness: 'overpayment'
                };
                sendMessageInfo(params).then(res => {
                    if (res.code === "0000") {
                        this.$Message.success(res.msg);
                        this.closeModal();
                    } else {
                        this.$Message.fail(res.msg);
                    }
                });
            }
        }
    };
</script>
